<html>
<head><meta charset="utf-8"><title>uninhabited enum dead code elimination · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited.20enum.20dead.20code.20elimination.html">uninhabited enum dead code elimination</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="178716337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited%20enum%20dead%20code%20elimination/near/178716337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited.20enum.20dead.20code.20elimination.html#178716337">(Oct 22 2019 at 01:58)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> Following up from this comment <a href="https://github.com/rust-lang/rust/pull/64890#discussion_r333612125" target="_blank" title="https://github.com/rust-lang/rust/pull/64890#discussion_r333612125">here</a>, I've started working on implementing an optimization pass to remove dead code when switching on an enum with uninhabited variants. This is my first time implementing a MIR optimization from scratch so feedback is definitely appreciated! How does this look? <a href="https://github.com/rust-lang/rust/compare/master...wesleywiser:uninhabited_enum_variants_pass?expand=1" target="_blank" title="https://github.com/rust-lang/rust/compare/master...wesleywiser:uninhabited_enum_variants_pass?expand=1">https://github.com/rust-lang/rust/compare/master...wesleywiser:uninhabited_enum_variants_pass?expand=1</a></p>



<a name="178738135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited%20enum%20dead%20code%20elimination/near/178738135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited.20enum.20dead.20code.20elimination.html#178738135">(Oct 22 2019 at 09:34)</a>:</h4>
<p>I like it. The actual elimination is a bit convoluted, but I see no way around it. Maybe the <code>values</code> and <code>targets</code> fields of the <code>SwitchInt</code> variant could be merged into a single list, then one could use <code>retain</code> on the list</p>



<a name="178738174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited%20enum%20dead%20code%20elimination/near/178738174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited.20enum.20dead.20code.20elimination.html#178738174">(Oct 22 2019 at 09:35)</a>:</h4>
<p>the <code>otherwise</code> branch would then need a custom field, but that would be fine I think</p>



<a name="178738207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited%20enum%20dead%20code%20elimination/near/178738207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited.20enum.20dead.20code.20elimination.html#178738207">(Oct 22 2019 at 09:35)</a>:</h4>
<p>not sure how much other could would be affected by that (I think there's some code that just wants to know about all targets, but that could be helped by creating an accessor function returning an iterator)</p>



<a name="178739703"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited%20enum%20dead%20code%20elimination/near/178739703" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited.20enum.20dead.20code.20elimination.html#178739703">(Oct 22 2019 at 09:57)</a>:</h4>
<p>Thanks!</p>
<blockquote>
<p>I like it. The actual elimination is a bit convoluted, but I see no way around it. Maybe the <code>values</code> and <code>targets</code> fields of the <code>SwitchInt</code> variant could be merged into a single list, then one could use <code>retain</code> on the list</p>
</blockquote>
<p>Do you want me to start working on that as part of this change or leave it for another time?</p>



<a name="178740474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited%20enum%20dead%20code%20elimination/near/178740474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited.20enum.20dead.20code.20elimination.html#178740474">(Oct 22 2019 at 10:08)</a>:</h4>
<p>We can leave it for later. Please open an issue about it or leave a FIXME on the fields</p>



<a name="178740882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited%20enum%20dead%20code%20elimination/near/178740882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/uninhabited.20enum.20dead.20code.20elimination.html#178740882">(Oct 22 2019 at 10:13)</a>:</h4>
<p>Ok. I'll open a PR with this and get a perf run started.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>